package jsonpath;

import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.stream.Stream;
import net.minidev.json.JSONObject;

public class JsonPathTest {

    public static void main(String[] args) {

        try (Stream<String> fileStream = Files.lines(Paths.get("/Users/dingjianfeng/Work/github/dubbo-samples/dubbo-samples-api/pom.xml"), Charset.defaultCharset())) {

            fileStream.flatMap(line -> Arrays.stream(line.split(" "))).distinct().forEach(System.out::println);
        } catch (Exception e) {

        }
        String jsonObj = "{\"MachineCode\":\"C3060300FFFBEBBF10c5b39a-145e-47cb-8cfc-8bd203567a2f\",\"ProductName\":\"2020\",\"CustomerName\":\"0430-89\",\"AuthContent\":{}}";
        String jsonNewValue = "{\"MaskColumns\":20000,\"SeniorStatus\":\"on\",\"ExtendsAuth\":[{\"MaxInstance\":2,\"ExpireDate\":\"2020-09-02\",\"Assets\":\"KUDU\"},{\"MaxInstance\":2,\"ExpireDate\":\"2020-09-02\",\"Assets\":\"HBASE\"}],"
            + "\"BasicAuth\":{\"MaxInstance\":10,\"ExpireDate\":\"2020-06-02\",\"Assets\":[\"ORACLE\",\"MYSQL\"]}}";
        DocumentContext ext = JsonPath.parse(jsonObj);

        JsonPath resourceInfoPath = JsonPath.compile("$.['AuthContent']");
        HashMap jsonObject = resourceInfoPath.read(jsonObj);

        jsonObject.put("Customized", jsonNewValue);

        ext.set(resourceInfoPath, jsonObject);
        System.out.println("====1=====");
        System.out.println(org.apache.commons.lang3.StringEscapeUtils.unescapeJson(ext.jsonString()));

        String newSingle = "'Customized':{'MaskColumns':20000,'SeniorStatus':'on','ExtendsAuth':[{'MaxInstance':2,'ExpireDate':'2020-09-02','Assets':'KUDU'},{'MaxInstance':2,'ExpireDate':'2020-09-02','Assets':'HBASE'}],"
            + "'BasicAuth':{'MaxInstance':10,'ExpireDate':'2020-06-02','Assets':['ORACLE','MYSQL']}}";

        ext.set(resourceInfoPath, newSingle);

        System.out.println("====2=====");
        System.out.println(org.apache.commons.lang3.StringEscapeUtils.unescapeJson(ext.jsonString()));
    }
}
